package concurrency_design_model

import (
	"sync"
	"sync/atomic"
)

// 用互斥锁来保护一个数值型的共享资源麻烦且效率低下。标准库的sync/atomic包对原子操作提供了丰富的支持
var total uint64

func worker3(wg *sync.WaitGroup) {
	defer wg.Done()
	var i uint64
	for i = 0; i <= 100; i++ {
		atomic.AddUint64(&total, i)
	}
}

func Worker3Test() {
	var wg sync.WaitGroup
	wg.Add(2)
	go worker3(&wg)
	go worker3(&wg)
	wg.Wait()
}
